/**
 * Created by yangfan on 2016/10/19.
 */
define(['config/appregister',
    "i18n",
    "business/services/httpServices",
    "business/library/services/httpServices",
    "business/home/services/filterService",
    "business/services/rightComponents",
    "business/services/filter"], function (app, i18n, httpService,libHttpServices) {
    app.controller("libraryMainCtrl", function ($scope, $state,DetailFilter,$rootScope) {
        $scope.$parent.isDetailPage = false;
        $scope.$parent.isMainpage = true;

        $scope.selectType = '所有格式';
        $scope.allType = "所有格式";
        //nav 区域设定
        $scope.showNavBox = false;
        $scope.navClass = "f-nav-out";

        //鼠标进入 改变导航区样式
        $scope.changeNav = function(curClass){
            if(curClass=$scope.navClass){
                $scope.navClass = "f-nav-enter";
            }
            $scope.showNavBox = true;
        };
        //热搜词跳转
        $scope.jumpToSearch = function(value){
            $rootScope.GLOBALKEYWORD =  value;
            sessionStorage.searchKeyWord = value;
            $state.transitionTo("search.library", {}, {
                reload: true, inherit: false, notify: true
            });
        }


        //鼠标移除 改变导航区样式
        $scope.mouseleave = function(mouseover){
            $scope.showNavBox = false;
            $scope.navClass = "f-nav-out"
        };

        //后台查询数据或缓存中查询数据。
        $scope.data = {
            "username":"Da Anna"
        };
        $scope.placeholder = "请输入您要输入的内容";
        $scope.mainData = {
            logoSrc:"them/images/gcj_big_logo.png"
        };
        $scope.isDetailPage = false;
        $scope.isMainpage = true;

        //图书馆
        $scope.i18n = i18n;
        $scope.curFileType = "";
        $scope.curParams = {} ;
        //点击登录
        $scope.login = function(){
            $state.go("login");
        };

        //详情列表参数
        $scope.listParams = {
            currentTcId: "",
            currentScId:"",
            download: false,
            time: false,
            pageNo: 1,
            pageSize: 10,
            all: false,
            typeId: "",
            scId: "",
            dcId: "",
            keyWord:"",
            //dcName scName 用于加入缓存 在详情中显示面包树导航
            dcName:"",
            scName:""
        };

        //后台加载的list数据
        $scope.list = [];
        function getListModel(){
            $scope.paginationConf = {
                currentPage: $scope.listParams.pageNo,
                totalItems: $scope.totalItem,
                //每页预览条数
                itemsPerPage:$scope.listParams.pageSize,
                //分页总数
                numberOfPages:Math.ceil($scope.totalItem/$scope.listParams.pageSize),
                // 实际分页长度
                pagesLength:5,
                perPageOptions: [10, 20, 30, 40, 50],
                onChange: function (currentPage) {
                    if(currentPage){
                        $scope.paginationConf.currentPage = currentPage;
                    }
                    $scope.list = [];
                    $scope.listParams.pageNo = $scope.paginationConf.currentPage;
                    getThesisList();
                }
            };
        };



        //获取下载排行数据f
        function getDownLoadRank() {
            var req = {
                type: "GET",
                data:{
                    count:10,
                },
                uri:"getDownLoadRank"
            };
            libHttpServices.getDownLoadRank(req).success(function (response) {
                if (!response.data) {
                    return
                }
                $scope.downloadRank = response.data;
                getDownLoadRankConf($scope.downloadRank);

            }).error(function () {
            })
        }
        function getDownLoadRankConf(data){
                $scope.setConf = {
                    data:data,
                    idType:"thesisId",
                    state:"eng.libraryDetail"
                }
        }
        //获取热搜词
        function getKeyWord() {
            var req = {
                type: "GET",
                data:{
                    count:10
                },
                uri:"getKeyWord"
            };
            libHttpServices.getKeyWord(req).success(function (response) {
                if (!response.data) {
                    return
                }
                $scope.keyWord = response.data;
            }).error(function () {
            })
        }
        getDownLoadRank();
        getKeyWord();


        /*
        * 论文 图书馆
        * */


        //getTop
        function getTopCategory (){
            var req = {
                type:"GET",
                uri:"getTopCategory"
            };
            libHttpServices.getTopCategory(req).success(function(response){
                if(!response.data)return ;
                $scope.$apply(function(){
                    $scope.libTopCategroy = response.data[0];
                    $scope.listParams.tcId =  response.data[0].tcId;
                    getSecCategory ();
                });
            }).error(function(data){
               console.log("请求错误");
            });
        };

        //getSecCategory
        function getSecCategory (){
            var req = {
                type:"GET",
                uri:"getSecondaryCategory",
                data:{
                    tcId :$scope.listParams.tcId
                }
            };
            libHttpServices.getSecondaryCategory(req).success(function(response){
                if(!response.data) return ;
                $scope.$apply(function(){
                    $scope.libSecCategory = response.data;
                   // $scope.listParams.scId =  response.data[0].scId;
                   // getDetailCategory();
                    if(!!$scope.curParams.scId){
                        $scope.clickCategory($scope.curParams)
                    }
                   else{
                        $scope.clickDataClassifyAll()
                    }
                });
            }).error(function(

            ){})

        }
        function getDetailCategory(){
            var req = {
                type:"GET",
                uri:"getDetailCategory",
                data:{
                    scId :$scope.listParams.scId
                }
            };
            libHttpServices.getDetailCategory(req).success(function(response){
                if(!response.data) return ;
                $scope.$apply(function(){
                    var objtemp = {
                        dcId:null,
                        dcName:"全部",
                        scId:$scope.listParams.scId,
                        sequence:0
                    };
                    response.data.unshift(objtemp);
                    $scope.libDateilCategory = response.data;
                    $scope.listParams.dcId = null;
                    if(!!$scope.curParams.dcId){
                        $scope.clickDeatilCategory($scope.curParams);
                    }else{
                        getThesisList();
                    }
                });
            }).error(function(){})
        }

        //getThesis List
        function getThesisList(){
            var req = {
                type:"GET",
                uri:"getList",
                data:{
                    tcId:$scope.listParams.tcId,
                    scId:$scope.listParams.scId,
                    dcId:$scope.listParams.dcId,
                    typeId:$scope.listParams.typeId,
                    keyWord:$scope.listParams.keyWord,
                    download:$scope.listParams.download,
                    time:$scope.listParams.time,
                    pageNo: $scope.listParams.pageNo,
                    pageSize:$scope.listParams.pageSize
                }
            };
            libHttpServices.getThesisList(req).success(function(response){
                if(!response)return;
                $scope.$apply(function(){
                    $scope.ThesisList = response.data.list;
                    $scope.totalItem = response.data.Pagination.totalCount;
                    getListModel();
                });
            }).error(function(response){

            });
        }

        //getTyleList
          function getFileTypeList(){
              var req = {
                  type:"GET"
              };
              libHttpServices.getTypeList(req).success(function(response){
                  if(!response) return ;
                  $scope.typeList = response.data;
              }).error(function(data){ })
          }

        //计算列表图片
        $scope.getPicType = function (typeId){
            if(!typeId) return ;
            if( typeId == 1){
                return "them/img/pdf.png"
            }
            else {
                return  "them/img/pdf.png"
            }
        };
        //getTopCategory();
        getFileTypeList();
        $scope.isShow = {
            showDataClassify: [],
            showFileType: [],
            //资料三级分类 显示flag
            showDetailClassify:[],
            showDetailBox:true,
            allUnderLine:false,
            allTypeUnderLine:true

        };
        $scope.listParams.typeId = null;
        // 点击二级目录
        $scope.clickCategory = function (item,event) {
            $scope.listParams.pageNo=1;
            //清空显示下标线
            $scope.isShow.showDataClassify = [];

            //显示detail Box
            $scope.isShow.showDetailBox = true;

            //清除all 情况下的  下标线
            $scope.isShow.allUnderLine  =false;
            //计算偏移量
            if(!!event){
                var  keyValue = event.srcElement ? 'srcElement' : 'target';
                var dis = event[keyValue].parentElement.offsetLeft - event[keyValue].parentElement.parentElement.offsetLeft;
                if (event[keyValue].parentElement.className == "f-engMain-cate-list clearfix" || event[keyValue].parentElement.className == "f-engMain-content-list ng-scope") {
                    return
                }
                $scope.positionLeft = dis + "px";
                $scope.triangleLeft = dis + "px";
            }

            if (!item) {
                return
            } else {
                $scope.listParams.scId = item.scId;
                var req = {
                    type: "GET",
                    data: {
                        scId: $scope.listParams.scId
                    },
                    uri: 'getDetailCategory'
                };
                getDetailCategory(req);
                //$scope.isShow.showDataClassify[index] = true;
                //详情分类总“全部”重置，
                $scope.isShow.showDetailClassify = [];
                $scope.isShow.showDetailClassify[0] = true;

            }
        };


        //当前scId  和 dcId  只有在点击detail Category 之后才会确定 。
        $scope.clickDeatilCategory = function (detail) {
            if (!detail) {
                return
            }
            $scope.isShow.showDetailClassify = [];
           // $scope.isShow.showDetailClassify[index] = true;
            $scope.listParams.scId = detail.scId;
            $scope.listParams.dcId = detail.dcId;
            getThesisList();
        };

        //选择文件格式 “全部” 以外 其他选项
        $scope.curFileType = "";
        $scope.checkFileType = function (item,index) {
            $scope.selectType=item.typeName;
            $scope.isShow.allTypeUnderLine = false;
            $scope.isShow.showFileType = [];
            $scope.isShow.showFileType[index] = true;
            $scope.listParams.typeId = item.typeId;
            getThesisList();
        };

        // 选择文件格式  "全部"
        $scope.selectAllFileType = function(){
            $scope.listParams.pageNo=1;
            $scope.selectType = $scope.allType;
            $scope.isShow.showFileType = [];
            // 显示 全部下标线
            $scope.isShow.allTypeUnderLine = true;
            // 请求参数为  null 时  会在 ajax 请求 方法中过滤
            $scope.listParams.typeId = null;
            getThesisList();
        };
        $scope.clickDataClassifyAll = function(){
            $scope.isShow.showDataClassify = [];
            $scope.isShow.showDetailBox = false;
            $scope.isShow.allUnderLine = true;

            //二级分类 为空
            $scope.listParams.scId = null ;
       //     $scope.listParams.tcId = null ;
            getThesisList();
        };

        // 点击搜索 搜索关键词
        $scope.searchKeyWord = function(){
            $scope.listParams.pageNo=1;
            getThesisList();
        };

        function init (){
            //判断从外部点击进入 JSON.parse(string)
            if(sessionStorage.hasOwnProperty("engParams") && ((toString.call(sessionStorage.engParams)=="[object Object]")||(toString.call(sessionStorage.engParams)=="[object String]"))&&sessionStorage.engParams!="") {
                if(toString.call(sessionStorage.engParams)=="[object Object]"){
                    $scope.curParams =  sessionStorage.engParams;
                }
                if(toString.call(sessionStorage.engParams)=="[object String]"){
                    $scope.curParams =  JSON.parse(sessionStorage.engParams);
                }
                sessionStorage.removeItem('engParams');
                getTopCategory();
            }else{
                getTopCategory();
            }
        };
        //初始化
        init();


        //点击导航列表
        $scope.positionLeft = 0;
        // 初始化 建筑位置 设置
        $scope.isShow.showDataClassify[0] = true;
        $scope.isShow.showDetailClassify[0] = true;
        //$scope.isShow.showFileType[0] = true;

        $scope.showA = function(index){
            $scope.isShow.showDataClassify = [];
            $scope.isShow.showDataClassify[index] = true;

        };
        $scope.mouseleave = function(index){
            //  $scope.isShow.showDataClassify[index] = false;
        };

        //点击时间 下载 按照这两种方式显示不同的图标 列表图标 图片图标
        var dropAndSort = {
            drop:"them/img/bottom.png",
            sort :"them/imag/top-blue-arrow.png"
        };
        $scope.picSrc = {
            theTime:dropAndSort.drop,
            download:dropAndSort.drop,
            list:"them/img/tablist.png",
            pic:"them/img/tabpic.png"
        };

        var changeTime = function(){
            if($scope.picSrc.theTime ==dropAndSort.drop){
                $scope.picSrc.theTime =dropAndSort.sort
            }else{
                $scope.picSrc.theTime =dropAndSort.drop
            }
        };
        var changeDownload = function(){
            if($scope.picSrc.download ==dropAndSort.drop){
                $scope.picSrc.download =dropAndSort.sort
            }else{
                $scope.picSrc.download =dropAndSort.drop
            }
        };

        //根据时间排序
        $scope.sortByTime = function () {
            $scope.listParams.pageNo=1;
            changeTime();
            $scope.listParams.time = !$scope.listParams.time;
            if ($scope.listParams.time && $scope.listParams.download) {
                $scope.listParams.download = false;
            }
            getThesisList();
        };
        //根据下载排序
        $scope.sortByDownload = function () {
            $scope.listParams.pageNo=1;
            changeDownload();
            $scope.listParams.download = !$scope.listParams.download;
            if ($scope.listParams.time && $scope.listParams.download) {
                $scope.listParams.time = false;
            }
            getThesisList();
        };
        //点击工程名 进入工程详情页
        $scope.enterEngDetail = function (item) {
            var state = {
                state:"eng.libraryDetail"
            };
            //sessionStorage.thesisId = item.thesisId;
            //sessionStorage.title = item.title;
            $state.go(state.state,{
                thesisId: item.thesisId
            });
        };
        $scope.stateGo =function(state){
            $state.go(state);
        };

        // 点击资料 显示下拉框
        $scope.isShowData = false;
        $scope.isInList = false;
        $scope.clickData = function(){
            if($scope.isShowData == false){
                $scope.isShowData = true;
            }
        };
        $scope.leaveSearchList =function(){
            if($scope.isShowData == true){
                $scope.isShowData = false;
            }
            $scope.isInList = true;
        };


    })
});

